package main;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 谢焕涛
 * @Description: FlexibleReplenishmentSolver
 * @Date: 2025/5/13 上午9:01
 * @Version: 1.0
 */
public class FlexibleReplenishmentSolver {

    // 参数配置类（可根据需要扩展）
    static class SolverConfig {
        int xMin = 7;
        Integer xMax = null;  // 动态计算
        int yMin = 10;
        Integer yMax = null;  // 动态计算
        int zMin = 1;
        Integer zMax = null;  // 动态计算
    }

    public static void main(String[] args) {
        SolverConfig config = new SolverConfig();
        List<String> solutions = solveInequality(config);
        printResults(solutions);
    }

    // 核心求解逻辑
    private static List<String> solveInequality(SolverConfig config) {
        List<String> validCombinations = new ArrayList<>();

        // 动态计算 z 的最大值: 3z/20 <1 → z <6.666
        int zMax = (config.zMax != null) ? config.zMax : (int) Math.floor(20.0 / 3);
        for (int z = config.zMin; z <= zMax; z++) {

            // 动态计算 y 的最大值: y <33*(1 - 3z/20)
            double rhsForY = 1.0 - (3.0 * z) / 20.0;
            int yMaxDynamic = (int) Math.floor(rhsForY * 33);
            int yMax = (config.yMax != null) ? Math.min(config.yMax, yMaxDynamic) : yMaxDynamic;

            for (int y = config.yMin; y <= yMax; y++) {

                // 动态计算 x 的最大值: x <20*(1 - y/33 - 3z/20)
                double rhsForX = rhsForY - (y / 33.0);
                int xMaxDynamic = (int) Math.floor(rhsForX * 20);
                int xMax = (config.xMax != null) ? Math.min(config.xMax, xMaxDynamic) : xMaxDynamic;

                // x 的最小值由配置决定
                for (int x = config.xMin; x <= xMax; x++) {
                    if (isValidCombination(x, y, z)) {
                        validCombinations.add(formatSolution(x, y, z));
                    }
                }
            }
        }
        return validCombinations;
    }

    // 验证不等式是否成立
    private static boolean isValidCombination(int x, int y, int z) {
        final double threshold = 1.0 - 1e-6; // 处理浮点精度误差
        return (x / 20.0) + (y / 33.0) + (3.0 * z / 20.0) < threshold;
    }

    // 格式化输出
    private static String formatSolution(int x, int y, int z) {
        return String.format("x=%-2d, y=%-2d, z=%-2d  校验值=%.6f",
                x, y, z,
                (x/20.0) + (y/33.0) + (3.0*z/20.0)
        );
    }

    // 打印结果
    private static void printResults(List<String> solutions) {
        System.out.println("共找到 " + solutions.size() + " 种组合：");
        solutions.forEach(System.out::println);
    }
}
